package com.my.demo.controller;


import com.my.demo.config.WechatMessageUtils;
import com.my.demo.domain.A;
import com.my.demo.domain.TestSummaryReport;
import com.my.demo.domain.User;
import com.my.demo.domain.selectSummaryVo;
import com.my.demo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apiguardian.api.API;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.annotation.Scope;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.support.HttpRequestHandlerServlet;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.net.URLEncoder;
import java.util.*;
import java.util.List;

/**
 * springboot访问web页面
 * 如何启动boot后 输入ip or ip+地址访问指定的如login index等页面?
 * 不能用RestController  该注解复合了ResponseBody注解 会直接访问string
 */
@RestController
//@Controller
//默认单例 每次访问都会递增 所以spring中 abc 是共享变量 输入000 111 222
@Scope("prototype")//多例 a和b 每次访问都是0  但是c会递增(static全局共享变量) 输出结果为000 001 002
public class MyController {

    int a = 0;
    private int b = 0;
    static int c = 0;
    @Resource
    UserService userService;

    @GetMapping("testabc")
    public void testabc(int a) {
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        a++;b++;
        c++;
    }

    @GetMapping("test11")
    public void test11(int a) throws InterruptedException {
        Thread.sleep(10000);
    }
    @GetMapping("test22")
    public String test22() {
        System.out.println(1);
        return "ok";
    }

    @PostMapping("test33")
    public void test33(A a) {
        System.out.println(a);
    }

//    public MyController(UserService userService) {
//        this.userService = userService;
//    }

    //templates文件夹下面的需要用controller下的接口跳转访问 配合依赖thymeleaf
    //static 目录下可以直接访问 如 localhost/login1.html
    @GetMapping("hello")
    public String hello() {
        return "login";
    }
    @GetMapping("/")
    public String hello1() {
        return "login";
    }

    @Autowired
    MessageSource messageSource;

    @RequestMapping("/test")
    @Transactional
    public String test() {
        List<User> list = userService.list();
        System.out.println(list);
        userService.test();
        String message = null;

        try {
            //i18n
            message = messageSource.getMessage("login.name", null, LocaleContextHolder.getLocale());
        } catch (NoSuchMessageException e) {
            message = "";
            System.out.println(e.getMessage());
        }
        return message;
    }

    //通过@SelectProvider 注解实现无xml文件的条件查询
    @RequestMapping("/test1")
    public List<User> test1() {
        selectSummaryVo selectSummaryVo = new selectSummaryVo();
        selectSummaryVo.setAge("1");
        List<User> selectSummaryList = userService.getSelectSummaryList(selectSummaryVo);
        return selectSummaryList;

    }



    @RequestMapping("/saveOrupdate")
    public String saveOrupdate() {
        List<User> users = new ArrayList<>();
        User user = new User();

        User user1 = new User();
        user1.setId(1);
        user1.setName("2");
//        user1.setAge(1);
//        user1.setDetail("1");
//        users.add(user);
        User user2 = new User();
        user2.setName("2");
        user2.setAge(2);
        user2.setDetail("2");
//        users.add(user);
        users.add(user1);
        users.add(user2);
        boolean b = userService.saveOrUpdateBatch(users);


        return "";
    }




}
